package com.example.module_lzq_haibathree.activity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.module_lzq_haibathree.BR;
import com.example.module_lzq_haibathree.R;

import com.example.module_lzq_haibathree.base.BaseMvvmActivity;
import com.example.module_lzq_haibathree.databinding.MyActivityHpSleepMonitorBinding;
import com.example.module_lzq_haibathree.utils.AudioRecordDemo;
import com.example.module_lzq_haibathree.utils.SleepFocusViewModel;

import com.fwlst.lib_base.utils.AdUtils;
import com.fwlst.lib_base.utils.BaseUtils;
import com.fwlst.lib_base.utils.JtXXPermissions;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class HpSleepMonitorActivity extends BaseMvvmActivity<MyActivityHpSleepMonitorBinding, SleepFocusViewModel> {
    private static final String TAG = "MainActivity";

    private static final int GET_RECODE_AUDIO = 1;
    private static final int refreshTime = 10;
    private static final String[] PERMISSION_AUDIO = {
            Manifest.permission.RECORD_AUDIO
    };
    private boolean mIsThreadRun;
    private AudioRecordDemo audioRecordDemo;
    private Thread mDectorthread;
    private InputStream ins;

    @Override
    public int initLayout() {
        return R.layout.my_activity_hp_sleep_monitor;
    }

    @Override
    public int initBR() {
        return BR.data;
    }

    @Override
    protected void initView() {
        BaseUtils.INSTANCE.setStatusBarLucency(this);
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(this, binding.bannerContainer);
        //verifyAudioPermissions(this);
        audioRecordDemo = new AudioRecordDemo(this);
        //返回按钮
        binding.returnTb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.startBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                List<String> pre = new ArrayList<>();
                pre.add(Manifest.permission.RECORD_AUDIO);
                JtXXPermissions.INSTANCE.requestPermission(HpSleepMonitorActivity.this, "", pre, new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        viewModel.setCurrentType(2);
                        return null;
                    }
                });

            }
        });
        //监听按钮点击
        viewModel.getCurrentType().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer == 0) return;
                switch (integer) {
                    case 1://点击了结束
                        audioRecordDemo.stop();
                        break;
                    case 2://点击了开始
                        audioRecordDemo.getNoiseLevel(new AudioRecordDemo.MonitorInterface() {
                            @Override
                            public void monitorListener(int res) {
                                binding.sleepFocusTimeText.setText(res + "");
                                if (res <= 1) {
                                    binding.breatheTv.setText("没有声音");
                                } else if (res <= 40) {
                                    binding.breatheTv.setText("比较理想的安静环境");
                                } else if (res <= 60) {
                                    binding.breatheTv.setText("一般普通室内谈话");
                                } else if (res <= 70) {
                                    binding.breatheTv.setText("吵闹、有损神经");
                                } else if (res <= 90) {
                                    binding.breatheTv.setText("很吵、神经细胞受到破坏");
                                } else if (res <= 100) {
                                    binding.breatheTv.setText("吵闹加剧、听力受损");
                                } else {
                                    binding.breatheTv.setText("会感到痛苦");
                                }
                            }
                        });
                        break;
                }
            }
        });

    }

    /*
     * 申请录音权限*/
    public void verifyAudioPermissions(Activity activity) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSION_AUDIO, GET_RECODE_AUDIO);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        audioRecordDemo.stop();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}